package com.sam.security;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.config.ParentPackage;

import com.sam.core.BaseAction;
import com.sam.core.domain.Page;
import com.sam.security.domain.AsyncTreeNode;
import com.sam.security.domain.Resource;
import com.sam.security.service.SecurityService;

/**
 * 系统权限资源管理
 * @project EMan
 * @author Sam
 * @version
 * @date Feb 10, 2009 9:45:18 AM
 */
@ParentPackage("default")
public class ResourceAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private SecurityService securityService;

	private Resource resource;

	private List<Resource> list;

	public String delete() throws Exception {
		securityService.deleteResource(dataString);
		sendJSON(true);
		return "json";
	}

	public List<Resource> getList() {
		return list;
	}

	public Resource getResource() {
		return resource;
	}

	public String load() throws Exception {
		Page<Resource> page = new Page<Resource>(start, limit);
		try {
			if (query != null && !query.equals("")) {
				securityService.searchResource(query, page);
			} else if (resource == null || resource.getResourceId() != 0) {
				securityService.loadResource(resource, page);
			} else {
				securityService.filterResource(resource, page);
			}
			sendJSON(page.getTotalCount(), page.getResult());
		} catch (RuntimeException e) {
			log.warn("查询资源失败", e);
			sendJSON(false, "查询资源失败");
		}
		return "json";
	}

	public String query() throws Exception {
		List<Resource> resources = null;
		if (query != null && !query.equals("")) {
			resources = securityService.searchResource(query);
		} else {
			resources = securityService.loadResource(resource);
		}
		sendJSON(resources.size(), resources);
		return "json";
	}

	public String save() {
		if (dataString != null && !dataString.equals("")) {
			JSONArray jsonArr = JSONArray.fromObject(dataString);
			for (int i = 0; i < jsonArr.size(); i++) {
				JSONObject jsonObj = jsonArr.getJSONObject(i);
				Resource res = (Resource) JSONObject.toBean(jsonObj, Resource.class);
				securityService.updateResource(res);
			}
		}
		sendJSON(true);
		return "json";
	}

	public String search() throws Exception {
		List<Resource> resources = null;
		if (query != null && !query.equals("")) {
			resources = securityService.searchResource(query);
		} else {
			resources = securityService.loadResource(resource);
		}
		List<AsyncTreeNode> nodes = new ArrayList<AsyncTreeNode>();
		for (int i = 0; i < resources.size(); i++) {
			Resource res = resources.get(i);
			AsyncTreeNode node = new AsyncTreeNode();
			node.setId(String.valueOf(res.getResourceId()));
			node.setText(res.getResourceName());
			node.setLeaf(true);
			nodes.add(node);
		}
		sendJSON(nodes);
		return "json";
	}

	public void setList(List<Resource> list) {
		this.list = list;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}

	public String update() throws Exception {
		securityService.updateResource(resource);
		sendJSON(true);
		return "json";
	}

}
